package com.syz.mall.controller;

import com.syz.mall.annotation.Log;
import com.syz.mall.enums.LogOperType;
import com.syz.mall.pojo.Category;
import com.syz.mall.pojo.Product;
import com.syz.mall.pojo.query.CategoryQuery;
import com.syz.mall.service.ICategoryService;
import com.syz.mall.util.LayUITableResult;
import com.syz.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/getCategoryListPage")
    public String getCategoryListPage() {
        return "/ProductManage/Category/category_list";
    }
    @RequestMapping("/getCategoryAddPage")
    public String getCategoryAddPage() {
        return "/ProductManage/Category/category_add";
    }
    @RequestMapping("/getCategoryUpdatePage")
    public String getCategoryUpdatePage(Integer id, Model model){
        Category category=categoryService.selectById(id);
        model.addAttribute("category",category);
        String parentName="顶级父类";
        if(category.getParentId()!=0){
            Category parent=categoryService.selectTopById(id);
            parentName=parent.getName();
        }

        model.addAttribute("parentName",parentName);
        return "/ProductManage/Category/category_update";
    }

    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(CategoryQuery categoryQuery) {
        System.out.println("CategoryController.selectByPage");
        return categoryService.selectByPage(categoryQuery);
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        boolean isSuccess = categoryService.deleteAll(ids);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        boolean isSuccess = categoryService.deleteById(id);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Category category) {
        boolean isSuccess = categoryService.add(category);
        return isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
    }
    @RequestMapping("/selectAllTop")
    @ResponseBody
    public List<Category> selectAllTop(){
        return categoryService.selectAllTop();
    }

    @RequestMapping("/selectAllSec")
    @ResponseBody
    public List<Category> selectAllSec(Integer topId){
        return categoryService.selectAllSec(topId);
    }
    @RequestMapping("/selectTopById")
    @ResponseBody
    public Category selectTopById(Integer id){
        return categoryService.selectTopById(id);
    }
    @Log(title = "类别管理", operType = LogOperType.UPDATE)
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Category category){
        boolean isSuccess = categoryService.update(category);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Result update(Integer id,Integer status){
        boolean isSuccess = categoryService.changeStatus(id,status);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }
}
